package com.lanou.musicproject.SQLite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.musicproject.bean.DownloadedSong;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ztq on 15/11/2.
 */
public class MySQLiteData {
    private SQLiteDatabase db;
    private Context mContext;

    public MySQLiteData(Context c){
        mContext = c;
        MyManagerDatabase base = new MyManagerDatabase(mContext,"downloaded",null,1);
        db = base.getWritableDatabase();
    }


    //插入方法
    public long insert(String song,String singer,String image){
        ContentValues values = new ContentValues();
        values.put(MyManagerDatabase.column_song,song);
        values.put(MyManagerDatabase.column_singer,singer);
        values.put(MyManagerDatabase.column_image,image);

        long id = db.insert(MyManagerDatabase.downloaded,null,values);
        return id;
    }


    //删除方法
    public int delete(String whereClause,String[] whereArgs){
        int conut = db.delete(MyManagerDatabase.downloaded, whereClause, whereArgs);
        return conut;
    }

    //改动方法
    public int upDate(String song,String singer,String image,String whereClause,String[] whereArgs){
        ContentValues values = new ContentValues();
        values.put(MyManagerDatabase.column_song,song);
        values.put(MyManagerDatabase.column_singer,singer);
        values.put(MyManagerDatabase.column_image,image);

        int count = db.update(MyManagerDatabase.downloaded, values, whereClause, whereArgs);
        return count;
    }


    //查询方法
    public List<DownloadedSong> queue(String selection,String[] selectionArgs){

        List<DownloadedSong> downloadedSongs = new ArrayList<>();
        Cursor cursor = db.query(MyManagerDatabase.downloaded,null,
                selection,selectionArgs,null,null,null);

        while (cursor.moveToNext()){
            int id_index = cursor.getColumnIndex("_id");
            int song_index = cursor.getColumnIndex(MyManagerDatabase.column_song);
            int singer_index = cursor.getColumnIndex(MyManagerDatabase.column_singer);
            int image_index = cursor.getColumnIndex(MyManagerDatabase.column_image);

            String song = cursor.getString(song_index);
            String singer = cursor.getString(singer_index);
            String image = cursor.getString(image_index);

            DownloadedSong downloadedSong = new DownloadedSong(image,singer,song);
            downloadedSongs.add(downloadedSong);
        }


        return downloadedSongs;
    }
}
